/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.impl;

import org.picocontainer.MutablePicoContainer;

import picounit.features.classloader.ClassReloader;
import picounit.features.registry.RegistryEntries;
import picounit.features.registry.RegistryImpl;
import picounit.features.registry.Resolver;
import picounit.features.registry.SilentlyReplacingMutablePicoContainer;
import picounit.features.registry.TypeFactory;
import picounit.features.registry.RegistryImpl.PicoContainerInstantiator;
import previous.picounit.Should;
import previous.picounit.TestCase;
import previous.picounit.Verify;

public class ResolverTest extends TestCase {
	private final MutablePicoContainer picoContainer = injectCollaborator();
	private final TypeFactory typeFactory = injectCollaborator();
	private final PicoContainerInstantiator picoContainerInstantiator = injectCollaborator();
	private final RegistryEntries registryEntries = injectCollaborator();
	private final ClassReloader classReloader = injectCollaborator();
	
	private final Resolver resolver = new RegistryImpl(picoContainer, picoContainerInstantiator, registryEntries,
		classReloader, typeFactory);

	public void shouldRetrieveConstantsFromPicoContainerUsingNameAsComponentInstanceKey(Should should, Verify verify) {
		String value = "value";

		should.call(typeFactory.hasInstance(String.class))
			.andReturn(false);
		
		should.call(picoContainer.getComponentInstance("name"))
			.andReturn(value);

		should.expectAboveWhenTheFollowingOccurs();

		verify.that(resolver.get("name", String.class))
			.isEqualTo(value);
	}
	
	public void shouldReturnUnderlyingPicoContainerIfAMutablePicoContainerIsRequested(Verify verify) {
		verify.that(resolver.get(null, MutablePicoContainer.class))
			.isEqualTo(SilentlyReplacingMutablePicoContainer.wrap(picoContainer));
	}
	
	public void shouldReturnComponentInstanceOfTypeFromPicoContainerIfValueIsNotNull(Should should, Verify verify) {
		class SomeClass {}
		SomeClass someInstance = new SomeClass();

		should.call(typeFactory.hasInstance(SomeClass.class))
			.andReturn(false);

		should.call(picoContainer.getComponentInstanceOfType(SomeClass.class))
			.andReturn(someInstance);

		should.expectAboveWhenTheFollowingOccurs();

		verify.that(resolver.get(null, SomeClass.class))
			.isTheSameAs(someInstance);
	}
	
	public void shouldUsePicoContainerInstantiatorIfComponentMissingFromContainer() {
	}
}
